package com.example.optimizaapp;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.optimizaapp.domain.ManejadorIncidente;
import com.example.optimizaapp.domain.ManejadorUsuario;
import com.example.optimizaapp.domain.Utilities.Constants;
import com.example.optimizaapp.domain.Utilities.LoginUserService;


public class LoginActivity extends Activity {
	
	ManejadorUsuario mu = ManejadorUsuario.getManejadorUsuario();
	ManejadorIncidente mi = ManejadorIncidente.getManejadorIncidente();
	private ProgressDialog pd;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_login);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		 
		mi.crearClasificacionIncidentes();
		mi.llenarEstados();
		mi.llenarPrioridades();
		 
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mu.getUsuarioLogueado()!= null){
			mu.setUsuarioLogueado(null);
		}
		EditText loginNombre = (EditText) findViewById(R.id.txtNombreLogin);
		EditText loginPassword = (EditText) findViewById(R.id.txtPasswordLogin);
		loginNombre.setText("");
		loginPassword.setText("");
	}
	
	public void logUser (View view){
		EditText loginNombre = (EditText) findViewById(R.id.txtNombreLogin);
		EditText loginPassword = (EditText) findViewById(R.id.txtPasswordLogin);
		if (!loginNombre.getText().toString().equals("")|| !loginPassword.getText().toString().equals("")){
			String url = Constants.SERVICIO_LOGIN + "name=" + loginNombre.getText().toString() + "&password=" + loginPassword.getText().toString();
			
			LoginUserService loginUser = new LoginUserService(this, ProgressDialog.show(LoginActivity.this, "En Progreso", "Cargando"));
			loginUser.execute(url);	
		}else{
			new AlertDialog.Builder(this).setTitle("Error").setMessage("Debe llenar los campos requeridos").setNeutralButton("OK", null).show();
		}
	}
	
	public void verifyLoggedUser(){
		if (mu==null){
			mu=ManejadorUsuario.getManejadorUsuario();
		}
		
		if (mu.getUsuarioLogueado() != null){
			
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
		}else{
			
			new AlertDialog.Builder(this).setTitle("Error").setMessage("Usuario no existe").setNeutralButton("OK", null).show();
		}
	}
	

}
